package com.boilerCloud.dao.maintenance;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.boilerCloud.dao.BaseDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.maintenance.MaintenancePlan;
import com.boilerCloud.util.StringUtil;

@Repository
public class MaintenanceDao extends BaseDao {

	public void qryList(MaintenancePlan model, HzlqswPage<MaintenancePlan> page)throws Exception {
		String countsql = "select count(*) from maintenance_plan a "
				+ " left join d_device_info b on b.id = a.device_id"
				+ " where a.is_delete='n' ";
			
		String listSql = "select a.*,b.name deviceName,e.name customerName "
					+ " from maintenance_plan a "
					+ " left join d_device_info b on b.id = a.device_id"
					+ " left join app_user e on b.user_id = e.id" 
					+ " where a.is_delete='n' ";
		List<Object> params= new ArrayList<Object>();
		if(!StringUtil.isEmpty(model.getName())){
			countsql += " and a.name like ?";
			listSql += " and a.name like ?";
			params.add("%" + model.getName() +"%");
		}
		
		
		if(!StringUtil.isEmpty(model.getDeviceName())){
			countsql += " and b.name like ?";
			listSql += " and b.name like ?";
			params.add("%" + model.getDeviceName() +"%");
		}
	
		listSql += " order by id desc ";
		this.queryListByPage(countsql, params, listSql, params, page, MaintenancePlan.class);
		
	}

	public void edit(MaintenancePlan model) throws Exception{
		String sql = "update maintenance_plan set name=?,device_id=?,start_time=?,maintenance_time=?,"
				+ "maintenance_type=?,maintenance_cycle=?,"
				+ "advance_dispatch=?,maintenance_cycle_type=?,automatic=? ,"
				+ "type=?,status=?,next_date=?,next_date_order=?,finsh_date=? where id=?";
		List<Object> param = new ArrayList<>();
		param.add(model.getName());
		param.add(model.getDeviceId());
		param.add(model.getStartTimeStr());
		param.add(model.getMaintenanceTime());
		param.add(model.getMaintenanceType());
		param.add(model.getMaintenanceCycle());
		param.add(model.getAdvanceDispatch());
		param.add(model.getMaintenanceCycleType());
		param.add(model.getAutomatic());

		param.add(model.getType());
		param.add(model.getStatus());
		param.add(model.getNextDate());
		param.add(model.getNextDateOrder());
		param.add(model.getFinshDate());
		param.add(model.getId());
		this.jdbcTemplate.update(sql,param.toArray());
	}

	public MaintenancePlan getById(int id) throws Exception{
		String sql = "select a.*,b.name deviceName  from maintenance_plan a"
				+ " left join d_device_info b on b.id = a.device_id"
				+ " where a.id= ? limit 1";
		return this.queryForObject(sql, MaintenancePlan.class,id);
	}

	public void save(MaintenancePlan model)throws Exception {
		String sql = "insert into  maintenance_plan(name,device_id,start_time,maintenance_time,"
				+ "maintenance_type,maintenance_cycle,advance_dispatch,maintenance_cycle_type,"
				+ "create_time,modify_time,is_delete,automatic,type,status,next_date,next_date_order,finsh_date)"
				+ " values(?,?,?,?,?,?,?,?,now(),now(),'n',?,?,?,?,?,?)";
		List<Object> param = new ArrayList<>();
		param.add(model.getName());
		param.add(model.getDeviceId());
		param.add(model.getStartTimeStr());
		param.add(model.getMaintenanceTime());
		param.add(model.getMaintenanceType());
		param.add(model.getMaintenanceCycle());
		param.add(model.getAdvanceDispatch());
		param.add(model.getMaintenanceCycleType());
		param.add(model.getAutomatic());
		param.add(model.getType());
		param.add(model.getStatus());
		param.add(model.getNextDate());
		param.add(model.getNextDateOrder());
		param.add(model.getFinshDate());
		this.jdbcTemplate.update(sql,param.toArray());
		
	}

	public void del(int id) {
		String sql = "update maintenance_plan set is_delete = 'y' where id = ?";
		this.jdbcTemplate.update(sql,id);
		
	}

	public void updateStatus(int id, int status) throws Exception{
		// TODO Auto-generated method stub
		String sql = "update maintenance_plan set status = ? where id = ?";
		this.jdbcTemplate.update(sql,status,id);
		
	}

}
